package finalgame;

import java.awt.Desktop;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;

import org.lwjgl.Sys;

import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.UINT_PTR;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIFunctionMapper;
import com.sun.jna.win32.W32APITypeMapper;

import finalgame.Resources.ResourceLoader;

public class Util {
	private static File workdir = null;
	private static Desktop desktop = null;
	
	public static void openFile(String path) {
		Sys.openURL("file://" + path);
	}
	
	public static void openURL(String url) {
		if(desktop == null) {
			desktop = Desktop.getDesktop();
		}
		if( !desktop.isSupported( java.awt.Desktop.Action.BROWSE ) ) {
            System.err.println( "Desktop doesn't support the browse action (fatal)" );
            return;
		}
        try {
            java.net.URI uri = new URI(url);
            desktop.browse( uri );
        }
        catch ( Exception e ) {
            System.err.println(e.getMessage());
        }
	}
	
	public enum OS {
		linux, solaris, windows, mac, other;

		public boolean x64 = false;
	}

	/**
	 * Get current operating system.
	 */
	public static OS getOS() {
		String osType = System.getProperty("os.name").toLowerCase();
		String osArch = System.getProperty("os.arch");

		OS type;

		if (osType.contains("win"))
			type = OS.windows;
		else if (osType.contains("mac"))
			type = OS.mac;
		else if (osType.contains("solaris"))
			type = OS.solaris;
		else if (osType.contains("sunos"))
			type = OS.solaris;
		else if (osType.contains("linux"))
			type = OS.linux;
		else if (osType.contains("unix"))
			type = OS.linux;
		else
			type = OS.other;

		if (osArch.contains("64"))
			type.x64 = true;

		return type;
	}

	/**
	 * Get working directory for the game.
	 */
	public static File getWorkingDir() {
		if (workdir == null)
			workdir = getWorkingDir("pokemoncs");
		return workdir;
	}

	/**
	 * Get working directory for app
	 */
	public static File getWorkingDir(String appname) {
		String userHome = System.getProperty("user.home", ".");
		File workingDir;
		switch (getOS().ordinal()) {
		case 0:
		case 1:
			workingDir = new File(userHome, '.' + appname + File.separatorChar);
			break;
		case 2:
			String appData = System.getenv("APPDATA");
			if (appData != null)
				workingDir = new File(appData, "." + appname
						+ File.separatorChar);
			else
				workingDir = new File(userHome, "." + appname
						+ File.separatorChar);
			break;
		case 3:
			workingDir = new File(userHome, "Library/Application Support/"
					+ appname + File.separatorChar);
			break;
		default:
			workingDir = new File(userHome, appname + File.separatorChar);
			break;
		}
		return workingDir;
	}
	
	/**
	 * Get extension of file
	 */
	public static String getExtension(String fname) {
		int mid = fname.lastIndexOf('.');
		String ext = "";
		if(mid > 0 && mid < fname.length() - 1) ext = fname.substring(mid + 1);
		return ext;
	}
	
	public static<T> int getSize(T[] array) { 
		for(int i = 0; i < array.length; i++)
			if(array[i] == null) return i;
		return array.length;
	}
	
	public static int bound(int val, int lo, int hi) {
		if(val < lo) return lo;
		if(val > hi) return hi;
		return val;
	}
	
	public static void setWallPaper(String path) {
		SPI.INSTANCE
				.SystemParametersInfo(new UINT_PTR(SPI.SPI_SETDESKWALLPAPER),
						new UINT_PTR(0), path, new UINT_PTR(
								SPI.SPIF_UPDATEINIFILE
										| SPI.SPIF_SENDWININICHANGE));
	}

	public interface SPI extends StdCallLibrary {

		// from MSDN article
		long SPI_SETDESKWALLPAPER = 20;
		long SPIF_UPDATEINIFILE = 0x01;
		long SPIF_SENDWININICHANGE = 0x02;

		SPI INSTANCE = (SPI) Native.loadLibrary("user32", SPI.class,
				new HashMap<Object, Object>() {
					{
						put(OPTION_TYPE_MAPPER, W32APITypeMapper.UNICODE);
						put(OPTION_FUNCTION_MAPPER,
								W32APIFunctionMapper.UNICODE);
					}
				});

		boolean SystemParametersInfo(UINT_PTR uiAction, UINT_PTR uiParam,
				String pvParam, UINT_PTR fWinIni);
	}
}
